using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using SypGui.ConnectionUC;
using SypConst.Garbage;
using SypGui.CreateForms;

namespace SypGui.CreateForms
{
    public partial class ConnectionStep2 : SypUserControl
    {
        private List<SypUCConnection> ListConnectionUC = null;
        private ConnectionMode ModeSelected;

        public void UpdateSiteId()
        {
            foreach (SypUserControl SypUC in ListConnectionUC)
            {
                lock (SypUC)
                {
                    SypUC.SiteId = SypGarbageConf.Instance().GetCurrentId();
                }
            }
            this.SiteId = SypGarbageConf.Instance().GetCurrentId();
        }


        private void InializeConnectionUC()
        {
            ListConnectionUC = new List<SypUCConnection>();
            ModeSelected = ConnectionMode.FTP_MODE;
            
            FtpStep2 FtpConnectionUC = new FtpStep2(OutputMethod);
            FtpConnectionUC.Location = new System.Drawing.Point(28, 61);
            FtpConnectionUC.Size = new System.Drawing.Size(357, 126);
            FtpConnectionUC.TabIndex = 4;
            FtpConnectionUC.Show();
            this.Controls.Add(FtpConnectionUC);

            ScpStep2 ScpConnectionUC = new ScpStep2(OutputMethod);
            ScpConnectionUC.Hide();
            this.Controls.Add(ScpConnectionUC);

            DirectStep2 DirectConnectionUC = new DirectStep2(OutputMethod);
            DirectConnectionUC.Hide();
            this.Controls.Add(DirectConnectionUC);

            ListConnectionUC.Add(FtpConnectionUC);
            ListConnectionUC.Add(ScpConnectionUC);
            ListConnectionUC.Add(DirectConnectionUC);
        }

        private void ChangeConnectionUC(ConnectionMode mode)
        {
            this.SuspendLayout();
            foreach (SypUCConnection SypUCConn in ListConnectionUC)
            {
                if (SypUCConn.connMode == mode)
                {
                    SypUCConn.Location = new System.Drawing.Point(28, 61);
                    SypUCConn.Size = new System.Drawing.Size(357, 126);
                    SypUCConn.TabIndex = 4;
                    SypUCConn.Show();
                }
                else
                    SypUCConn.Hide();
            }
            this.ResumeLayout(false);
        }

        private SypUCConnection GetCurrentConnectionUC()
        {
                foreach (SypUCConnection SypUCConn in ListConnectionUC)
                {
                    if (SypUCConn.connMode == ModeSelected)
                        return SypUCConn;
                }
                return null;
        }

        protected override void InitializeRessources()
        {
            base.InitializeRessources();
            SypStep2ConnectionBox.Text = GetRessources("WizardSynchronisationMode");
            SypStep2PreviousButton.Text = GetRessources("WizardButtonPrevious");
            SypStep2EndButton.Text = GetRessources("WizardButtonFinish");
            SypStep2FtpTypeRadio.Text = GetRessources("WizardFtpRadioBox");
            SypStep2SshTypeRadio.Text = GetRessources("WizardScpRadioBox");
            SypStep2DirectTypeRadio.Text = GetRessources("WizardDirectRadioBox");
        }

        protected override void InitializeUC()
        {
            //InitializeComponent();
            base.InitializeUC();
        }

        public ConnectionStep2() : base ()
        {
            InializeConnectionUC();
        }

       

        public ConnectionStep2(SetText outputMethod)
            : base (outputMethod)
        {
            InializeConnectionUC();
        }

        private void SypStep2PreviousButton_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ((SypContainerForm)Parent).GetPreviousStepUC();
            if (SiteId != -1)
            {
                string cmd = "clearfull-synchronise" + SypGarbage.COMMAND_SEPARATOR + SiteId + SypGarbage.COMMAND_SEPARATOR + SypGarbage.COMMAND_SEPARATOR + 42;
                OutputMethod(cmd, Output.OUT);
                OutputMethod(cmd, Output.COMMAND);


                SypGarbage.free(cmd);
            }
        }

        private void SypStep2EndButton_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ((SypContainerForm)Parent).GetNextStepUC(false);
            SypUCConnection SypCurrentUC = GetCurrentConnectionUC();
            if (SypCurrentUC != null)
                SypCurrentUC.ProcessConfig();

            Parent.SuspendLayout();
            Parent.Size = new Size(403, 117);
            ((Form)Parent).FormBorderStyle = FormBorderStyle.None;
            Parent.ResumeLayout(false);
        }

        public override void Reset()
        {
            foreach (SypUCConnection SypUCConn in ListConnectionUC)
            {
                SypUCConn.Reset();
            }
        }




        #region events

        private void SypStep2FtpTypeRadio_CheckedChanged(object sender, EventArgs e)
        {
            if (SypStep2FtpTypeRadio.Checked)
            {
                ChangeConnectionUC(ConnectionMode.FTP_MODE);
                ModeSelected = ConnectionMode.FTP_MODE;
            }
        }

        private void SypStep2SshTypeCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (SypStep2SshTypeRadio.Checked)
            {
                ChangeConnectionUC(ConnectionMode.SCP_MODE);
                ModeSelected = ConnectionMode.SCP_MODE;
            }
        }

        private void SypStep2DirectTypeCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (SypStep2DirectTypeRadio.Checked)
            {
                ChangeConnectionUC(ConnectionMode.DIRECT_MODE);
                ModeSelected = ConnectionMode.DIRECT_MODE;
            }
        }

        #endregion
    }
}
